﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ACL.Comm;

namespace ACL.Supervisor
{
    enum beingHelped {
        Yes,
        no

    }

    public class AgentHelp
    {

       private   List<Tuple<AgentTracker,beingHelped>> AgentsNeedingAssistance = new List<Tuple<AgentTracker,beingHelped>> ();
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="help"></param>
       private void AddToNeedHelpList(AgentTracker help)
        {          
            AgentsNeedingAssistance.Add(new Tuple<AgentTracker,beingHelped>(help,beingHelped.no));
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="agentTracker"></param>
        public void HelpCompleted(AgentTracker agentTracker)
        {
            AgentsNeedingAssistance.Remove(AgentsNeedingAssistance.Where(b => b.Item1 == agentTracker).FirstOrDefault());
        }
       
        
        /// <summary>
       /// 
       /// </summary>
       /// <returns></returns>
        public List<Agent> GetAgents()
        {
            return AgentsNeedingAssistance.Select(b => b.Item1.agent).ToList();

        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
         public List<AgentTracker> GetAgentTrackers()
        {
             return AgentsNeedingAssistance.Select(b => b.Item1).ToList();

        }
        
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="agent"></param>
        /// <returns></returns>
        public AgentTracker FindAgentInNeed(Agent agent)
        {
             foreach (var skill in agent.Skills)
	            {
                 var check = AgentsNeedingAssistance.Where(b => b.Item2 == beingHelped.no && b.Item1.agent.Skills.Contains(skill)).Select(b => b.Item1);

                 if(check.Count() > 0)
                     return check.FirstOrDefault();		 
	            }

            //if nothing found return null
            return null;

        }
        
        
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="AgentInNeed"></param>
        /// <param name="agents"></param>
        /// <returns></returns>
           public Agent GetHelp(AgentTracker AgentInNeed, List<Agent> agents)
        {
            AddToNeedHelpList(AgentInNeed);


               foreach (var agent in agents.Where(b => b.Skills.Contains(AgentInNeed.skillthatneedsHelp)))
	            {

                   //make the agent who be offering help is not in the list himself
		            if(!GetAgents().Contains(agent))
                        return agent;
	            }        

            //if nothing found return null
            return null;

        }


 


    }
}